package com.lilosoft.business.precity.service;

import com.lilosoft.business.precity.dao.UpDataReconciliationMapper;
import com.lilosoft.business.precity.models.UpDataReconciliation;
import com.lilosoft.business.util.PreInitCdBatchUtil;
import com.lilosoft.core.utils.commons.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description: 数据对账表
 * User: 小峰
 * Date: 2019-08-29
 * Time: 11:15
 */
@Service
public class UpDataReconciliationService {
    @Autowired
    private UpDataReconciliationMapper upDataReconciliationMapper;

    @Autowired
    private PreInitCdBatchUtil preInitCdBatchUtil;
    @Autowired
    private UpProAcceptIndividualService upProAcceptIndividualService;
    @Autowired
    private UpProAcceptService upProAcceptService;
    @Autowired
    private UpProAcceptUnitService upProAcceptUnitService;
    @Autowired
    private UpProAttachmentService upProAttachmentService;
    @Autowired
    private UpProMaterialService upProMaterialService;
    @Autowired
    private UpProProcessService upProProcessService;
    @Autowired
    private UpProResultService upProResultService;
    @Autowired
    private UpProSpecialprocedureService upProSpecialprocedureService;

    public UpDataReconciliation createModel() {
        UpDataReconciliation model = new UpDataReconciliation();
        model.setId(UUIDUtils.createUUId());
        model.setCdUptime(new Date());
        model.setDelState(0);
        model.setSyncSign(0);
        model.setSystemSign("朝阳区政务服务局北京力龙公司");
        model.setDatasource("105");
        return model;
    }

    public void insertModel(UpDataReconciliation model) {

        upDataReconciliationMapper.insertSelective(model);
    }


    public void countdataByCDbatch() {
        String cdbatch = preInitCdBatchUtil.initCdBatch();
        UpDataReconciliation model =createModel();
        model.setCdBatch(cdbatch);
        //自然人信息
        Long individualCount = upProAcceptIndividualService.countByCDbatch(cdbatch);
        if(null !=individualCount && individualCount>0){
            model.setId(UUIDUtils.createUUId());
            model.setTablename("UP_PRO_ACCEPT_INDIVIDUAL");
            model.setCdCount(individualCount.intValue());
            insertModel(model);
        }
        //办件受理信息
        Long acceptCount = upProAcceptService.countByCDbatch(cdbatch);
        if(null !=acceptCount && acceptCount>0){
            model.setId(UUIDUtils.createUUId());
            model.setTablename("UP_PRO_ACCEPT");
            model.setCdCount(acceptCount.intValue());
            insertModel(model);
        }
        //申请法人信息
        Long unitCount = upProAcceptUnitService.countByCDbatch(cdbatch);
        if(null !=unitCount && unitCount>0){
            model.setId(UUIDUtils.createUUId());
            model.setTablename("UP_PRO_ACCEPT_UNIT");
            model.setCdCount(unitCount.intValue());
            insertModel(model);
        }
        //附件相关信息
        Long attachementCount = upProAttachmentService.countByCDbatch(cdbatch);
        if(null !=attachementCount && attachementCount>0){
            model.setId(UUIDUtils.createUUId());
            model.setTablename("UP_PRO_ATTACHMENT");
            model.setCdCount(attachementCount.intValue());
            insertModel(model);
        }
        //材料目录信息
        Long materialCount = upProMaterialService.countByCDbatch(cdbatch);
        if(null !=materialCount && materialCount>0){
            model.setId(UUIDUtils.createUUId());
            model.setTablename("UP_PRO_MATERIAL");
            model.setCdCount(materialCount.intValue());
            insertModel(model);
        }
        //办理过程信息
        Integer processCount = upProProcessService.countByRemark(cdbatch);
        if(null !=processCount && processCount>0){
            model.setId(UUIDUtils.createUUId());
            model.setTablename("UP_PRO_PROCESS");
            model.setCdCount(processCount.intValue());
            insertModel(model);
        }
        //办件结果信息
        Long resultCount = upProResultService.countByCDbatch(cdbatch);
        if(null !=resultCount && resultCount>0){
            model.setId(UUIDUtils.createUUId());
            model.setTablename("UP_PRO_RESULT");
            model.setCdCount(resultCount.intValue());
            insertModel(model);
        }
        //特别程序信息
        Long procedureCount = upProSpecialprocedureService.countByCDbatch(cdbatch);
        if(null !=procedureCount && procedureCount>0){
            model.setId(UUIDUtils.createUUId());
            model.setTablename("UP_PRO_SPECIALPROCEDURE");
            model.setCdCount(procedureCount.intValue());
            insertModel(model);
        }
        preInitCdBatchUtil.addCdBatch();
    }

}
